package com.horse.cloud.gateway.filter;

import com.alibaba.fastjson.JSON;
import com.horse.cloud.framework.general.enums.ResponseCodeEnums;
import com.horse.cloud.framework.general.utils.IPAddressUtils;
import com.horse.cloud.framework.redis.RedisClient;
import com.horse.cloud.gateway.constant.MicroServiceRequestConstant;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.http.HttpStatus;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.stereotype.Component;
import org.springframework.util.MultiValueMap;
import org.springframework.util.StringUtils;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

import java.net.URI;

/**
 * @create: Created by intelliJ IDEA18.1.3
 * @author: bing.Pan
 * @e-mail: 15923508369@163.com
 * @gmdate: 09/11/2018　16:49　星期五 (dd/MM/YYYY HH:mm)
 * @sidesc:
 */

@Component
public class AuthorizeFilter implements GlobalFilter, Ordered {

    private static final Logger LOGGER = LoggerFactory.getLogger(AuthorizeFilter.class);



    @Autowired private RedisClient redisClient;

    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        ServerHttpRequest request = exchange.getRequest();
        String method = request.getMethodValue();
        URI uri = request.getURI();
        String host = request.getURI().getHost();
        String ipRegion = IPAddressUtils.ipRegion(host);
        MultiValueMap<String, String> queryParams = request.getQueryParams();

        LOGGER.info("【cloud-gateway】 请求IP地址:[{}]",host);
        LOGGER.info("【cloud-gateway】 请求方法:[{}]",method);
        LOGGER.info("【cloud-gateway】 请求IP地址所属区域:[{}]",ipRegion);
        LOGGER.info("【cloud-gateway】 请求服务路径 [{}]", uri.getPath());
        LOGGER.info("【cloud-gateway】 请求参数 [{}]", JSON.toJSONString(queryParams));

        String token =  request.getQueryParams().getFirst(MicroServiceRequestConstant.AUTHORIZE_TOKEN);
        String uuid = request.getQueryParams().getFirst(MicroServiceRequestConstant.AUTHORIZE_UUID);

        ServerHttpResponse response = exchange.getResponse();
        if (StringUtils.isEmpty(token) || StringUtils.isEmpty(uuid)) {
            return GatewayResultAdapter.htmlAdapter(response, HttpStatus.UNAUTHORIZED);
        }
        String authToken = (String) redisClient.get(uuid);
        if (authToken == null || !authToken.equals(token)) {
            return GatewayResultAdapter.jsonAdapter(response, ResponseCodeEnums.SERVER_GATEWAY_FILTER_ERROR);
        }

        return chain.filter(exchange);
    }

    @Override
    public int getOrder() {
        return Ordered.LOWEST_PRECEDENCE;
    }



}
